package com.yuanjin.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class MyNetWorkUtils {
	public static int FriendsShow = 1;
	public static String urlString;
	public Handler MyNetWorkUtils_handler;

	/**
	 * @param Type
	 *            - 下载请求类型，MyNetWorkUtils.FriendsShow - 请求朋友圈
	 * @param handler
	 *            - 用于数据的传输
	 */
	public void downloadFile(int Type, Handler handler) {
		MyNetWorkUtils_handler = handler;
		switch (Type) {
		case 1:// 请求朋友圈
			urlString = "http://1.zhan414.sinaapp.com/?/index.php?c=main&a=downloadFriendsShowInfo";

			break;

		default:
			urlString = "http://1.zhan414.sinaapp.com/?/index.php?c=main&a=downloadFriendsShowInfo";

			break;
		}
		if (urlString != null) {
			Thread downloadThread = new Thread(new Runnable() {

				@Override
				public void run() {
					try {

						URL url = new URL(urlString);// 创建下载地址对应的URL对象
						HttpURLConnection urlConn = (HttpURLConnection) url
								.openConnection();// 创建一个连接
						urlConn.setConnectTimeout(3000);// 设置连接网络的超时时间
						int response = urlConn.getResponseCode();// 如果服务器完成了客户端请求则会返回一个状态码
						InputStream is = null;
						Log.e("testing",
								"--------Friendsshow下载数据 -- 请求------->response="
										+ response);
						if (response == 200) {// 表示成功
							Log.e("testing",
									"-----------Friendsshow下载数据 -- 请求成功--------->response="
											+ response);
							is = urlConn.getInputStream();// 获取输入流对象

						}

						/******************************************/
						InputStreamReader isr = new InputStreamReader(is);
						BufferedReader br = new BufferedReader(isr);
						String resultString = br.readLine().substring(2);// 从2切起去掉下传的多余数据？

						Log.e("testing",
								"---------Friendsshow下载返回数据  =  ------->>"
										+ resultString);
						/******************************************/

						is.close(); // 关闭输入流对象
						urlConn.disconnect(); // 关闭连接
						Message msg = new Message();
						msg.arg1 = 1000;
						msg.obj = resultString;
						MyNetWorkUtils_handler.sendMessage(msg);
					} catch (MalformedURLException e) {
						e.printStackTrace(); // 输出异常信息
					} catch (IOException e) {
						e.printStackTrace(); // 输出异常信息
					}

				}
			});
			downloadThread.start();
		}

	}

	/**
	 * @param inputString
	 *            json
	 */
	public void paseJsonInfo(String inputString) {
		try {
			JSONObject object = (JSONObject) new JSONTokener(inputString)
					.nextValue();// 将字符串转成jsonobject
			String name = object.getString("name");
			String value = object.getString("value");
			JSONArray pictureslink = object.getJSONArray("pictureslink");

			Log.e("testing", "------------result is =  ------------->>"
					+ "name = " + name + " value = " + value);
			Log.e("testing",
					"------------pictureslink.get(0); is =  ------------->>"
							+ pictureslink.get(0));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
